package admin;

import java.io.*;
import java.net.*;
import java.util.*;

import javax.servlet.http.*;

import model.*;

import org.apache.struts.action.*;
import org.apache.struts.actions.*;

public class BoardAction extends DispatchAction{
	
	public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) throws Exception {
		
		//Form취득
		BoardForm boardform = (BoardForm)form;
		
		int cono = Integer.parseInt(req.getParameter("cono"));
		String targetboardNm = req.getParameter("bkind");
		
		/*System.out.print(cono);
		System.out.println(targetboardNm);*/
		
		
		if(targetboardNm.equals("photo"))
		{
			boardform.setDispNm("사진");
		}
		else if(targetboardNm.equals("data"))
		{
			boardform.setDispNm("데이터");
		}
		else if(targetboardNm.equals("free"))
		{
			boardform.setDispNm("자유");
		}
		
		
		//회원 정보
		Vector<MemberCommunity> mVector = null;
		
		//멤버 테이블 매칭 DAO
		MemberCommunityDAO memDao = new MemberCommunityDAO();
		
		//******************************필요테이터취득
		mVector = memDao.getMemberCommunity(cono);
		//******************************완료
		boardform.setCono(""+cono);
		boardform.setTargetboardNm(targetboardNm);
		boardform.setMembers(mVector);
		
		return mapping.findForward("form");
		
		
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) throws Exception {
		
		//Form취득
		//tegetboardNm
		//cono
		//id
		System.out.println("호출");
		
		String mid =  URLDecoder.decode(req.getParameter("id"), "UTF-8");			//한글 깨짐 방지를 위한 디코딩
		String cono =  URLDecoder.decode(req.getParameter("cono"), "UTF-8");		//한글 깨짐 방지를 위한 디코딩
		String targetbd =  URLDecoder.decode(req.getParameter("tagetbd"), "UTF-8");	//한글 깨짐 방지를 위한 디코딩
		
		/*System.out.println(id);
		System.out.println(cono);
		System.out.println(targetbd);*/
		NoticeBoardDAO noticeDao = new NoticeBoardDAO();	//notice DAO
		FreeBoardDAO freeDao = new FreeBoardDAO();			//free DAO
		DataBoardDAO dataDao = new DataBoardDAO();			//data DAO
		PhotoBoardDAO photoDao = new PhotoBoardDAO();		//photo DAO
		
		
		res.setContentType("text/xml");
		res.setCharacterEncoding("UTF-8");
		

		String result = "";
		
		result += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
		result += "<result>";
		
		//=======데이터 삭제=========
		if(targetbd.equals("photo"))//사진
		{
			int pbno = -1;
			pbno = photoDao.getPbNo(Integer.parseInt(cono), mid);
			PhotoCommentDAO pcd = new PhotoCommentDAO();
			pcd.delete(pbno);
			photoDao.delete(Integer.parseInt(cono), mid);
			result +="<code>0</code>";
		
		}
		else if(targetbd.equals("data"))//데이터
		{
			int dbno = -1;
			dbno = dataDao.getDbNo(Integer.parseInt(cono), mid);
			DataCommentDAO dcd = new DataCommentDAO();
			dcd.deleteForData(dbno);
			dataDao.delete(Integer.parseInt(cono), mid);
			result +="<code>0</code>";
		}
		else if(targetbd.equals("free"))//자유
		{
			
			int fbno = -1;
			fbno = freeDao.getFbNo(Integer.parseInt(cono), mid);
			FreeCommentDAO fcd = new FreeCommentDAO();
			fcd.deleteAll(fbno);
			freeDao.delete(Integer.parseInt(cono), mid);
			result +="<code>0</code>";
		}
		
		//===========================
		result +="</result>";
		PrintWriter out = res.getWriter();
		out.print(result);
		out.close();
		return null;
	}

}
